package com.example.springbootkafkalearning.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * @author 罗俊华
 * @date 2021/12/17 - 4:01 下午
 */
@Slf4j
@RestController
@RequestMapping("/kafka")
public class KafkaController {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    public static final String TOPIC_NAME="spring-boot-topic";

    @GetMapping("/put/{msg}")
    public String putMsg(@PathVariable("msg")String msg) throws ExecutionException, InterruptedException {

        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(TOPIC_NAME, msg);

        return future.get().toString();
    }

}
